<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title>记事本</title>
    <link href="css/mui.min.css" rel="stylesheet"/>
    <link href="css/MyShare.css" rel="stylesheet"/>
</head>
<body>
	<header class="mui-bar mui-bar-nav">
	    <a class="mui-action-backs mui-icon mui-icon-gear mui-pull-right" id='settings'></a>
	    <h1 class="mui-title">记事本</h1>
	</header>
	
	 
	    
	 
	
	<div class="mui-content" id='list'>
	   	<!--div class='list_item height200 floatLeft padding5 breakAll w_33-3'>
	   		<div class='border height_100 positionRelative padding5'>
		   		<div >aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>
		   		<img src='image/delete.png' class='delete_item height25 positionAbsolute bottom10 right10' />
	   		</div>
	   	</div-->
	</div>
	  
	  
	  
	
	<div style='height:50px;clear:both'></div>
	<nav class="mui-bar mui-bar-tab">
	    <a class="mui-tab-item mui-active" id='addNewPage'>
	        <span class="mui-tab-label">添加新项</span>
	    </a>
	</nav>
</body>
</html>
<script src="js/mui.min.js"></script>
<script src="js/tools.js"></script>
<script src="js/md5.js"></script>
<script type="text/javascript" charset="utf-8">
mui.init(); 
//var path = '_doc/notepad';
 

mui.plusReady(function(){ 
	var shadowPath = plus.storage.getItem('shadowPath');
	var shadowFile= plus.storage.getItem('shadowFile');
	var path = plus.storage.getItem('notepad');
	var fullPath = shadowPath +'/'+ shadowFile;
	


	 
	//设置
	mui('#settings')[0].addEventListener('tap',function(){
		plus.webview.open('settings.html','settings.html');
	});
	
	  
	
	//初始化notepad 文件目录 或 遍历notepad目录中文件
	index_init();
	function index_init(){
		//return;
		getPathEntry(path,function(res){		//判断notepad目录是否存在，否则创建
			var list = document.getElementById('list');
			list.innerHTML='';
			list.innerHTML = '<div></div>';
			if(res.error){ 	//如果目录不存在则创建
				createDir(path,function(res){}); 
			}else{ 	//目录存在则遍历
				readDirs(res,function(res2){
					if(res2.files.length > 0){
						var tmp_val;
						res2.files.forEach(function(v,i,t){
							res2.files.forEach(function(v2,i2,t2){
								if(i2 < t.length-1){
									if(t2[i2].name > t2[i2+1].name){
										tmp_val = t2[i2+1];
										t2[i2+1] = t2[i2];
										t2[i2] = tmp_val;
									}									
								}
							})
						});

						res2.files.forEach(function(v,i,t){
							var filePath = plus.io.convertAbsoluteFileSystem(v.fullPath); 
							var div = document.createElement('div');
							//div.setAttribute('filePath',plus.io.convertLocalFileSystemURL(v.fullPath));
							div.onclick = function(){
								//plus.webview.open('','');
								mui.openWindow({
									url:'detail.html',
									id:'detail.html',
									extras:{
										filePath:filePath,
										title:v.name
									}
								})
							}
							div.className = 'list_item height170 floatLeft padding5 breakAll positionRelative w_33-3';
							div.innerHTML = '<div class="border height_100 positionRelative padding15"><div >'+v.name+'</div><img filePath="'+filePath+'" src="image/delete.png" class="delete_item height25 positionAbsolute bottom10 right10" /></div>';
							list.insertBefore(div,list.lastChild);  
						});
						delsFile();  
					}
				});
	 
			}
		})
	} 
	
	 
	//删除文件
	function delsFile(){
		var delete_item = document.getElementsByClassName('delete_item');
		for(var i=0;i<delete_item.length;i++){
			delete_item[i].onclick=function(e){
				
				
				var event=e || window.event;
				if(document.all){
					event.cancelBubble=true;
				}else{
					event.stopPropagation();
				}
				var filePath = this.getAttribute('filePath');
				plus.nativeUI.confirm(
					'确认删除?',
					function(e){
						if(e.index == 1){
							return;
						}
						getPathEntry(filePath,function(res){
							res.remove(
								function(){
									mui.toast('删除成功！');
									index_init();
								},
								function(e){
									alert(e.message);  
								}
							);
						});
					},
					'删除文件',
					['确认','取消']
				);
			};
		}
	}
	
	 
	
		

	
	 
	
	//添加新项
	document.getElementById('addNewPage').addEventListener('tap',function(){
		plus.nativeUI.prompt(
			' ',
			function(e){
				if(e.index == 0){
					var fileName = e.value.trim();
					if(fileName.length > 40){
						mui.alert('标题过长！');
						return;
					}
					if(!fileName){
						mui.toast('文件名不得为空！');return;
					}
					
					var filePath = path+'/'+fileName;
					getPathEntry(filePath,function(res){
						if(res.error){
							createFile(path,fileName,function(res){
								if(res.error){
									alert(res.msg);
								}else{
									mui.toast('创建成功！');
									index_init();  
								}
							});						
						}else{
							mui.toast('文件已存在！');
						}
					});
				}
			},
			"添加新项",
			"请输入文件名", 
			["确认","取消"]
			
		);
	});
	
	

	
	

})
</script>